using System;
using System.Collections.Generic;
using System.Text;
using Calc.Execution;

namespace Calc.Operations
{
	public class Variable : IOperand
	{
		protected string _name;
		protected double _value;

		public Variable()
			: this("", double.MinValue)
		{
		}

		public Variable(string name)
			: this(name, double.MinValue)
		{
		}

		public Variable(string name, double value)
		{
			_name = name;
			_value = value;
		}

		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public double Value
		{
			get { return _value; }
		}


		public virtual double GetValue(ExecutionContext context)
		{
			// check execution context for an updated version of this variable
			if ( context.Variables.ContainsKey(_name) )
				_value = context.Variables[_name];
			else 
				throw new ExecutionException("The variable " + _name + " is undefined");

			return _value;
		}

		public override string ToString()
		{
			return _name;
		}
	}
}
